<?php
namespace Biz\validate\activity;

use Doraemon\pockets\validate\Base;
use Biz\service\Goods as sGoods;
use Biz\model\activity\FlashSale as mFlashSale;
use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\service\activity\AllActivityInfo;
use function GuzzleHttp\Promise\all;
use Biz\pockets\constInterface\ActivityInfo;
use Biz\service\activity\AllactivityInfoBak;

class FlashSale extends Base
{
	public static function rules()
	{
		return [
			['goods_id', 'required', null, '请选择商品',['searchgoods', 'save']],
			['goods_id', 'GreaterThan', ['min' => 1, 'inclusive' => FALSE], '商品ID不合法',['searchgoods', 'save']],
			['goods_id', 'Callback', ['callback' => [new self(), 'checkGoodsId']], '商品状态不正确',['searchgoods', 'save']],
			['goods_id', 'Callback', ['callback' => [new self(), 'checkGoodsActivityTimer']], '商品在相同时间内存在其他活动',['searchgoods', 'save']],
			['start_time', 'required', null, '请选择开始时间',['searchgoods', 'save']],
			['end_time', 'required', null, '请选择结束时间',['searchgoods', 'save']],
			['end_time', 'Callback', ['callback' => [new self(), 'checkTimer']], '请检查时间范围是否与其他限时购活动重叠',['searchgoods', 'save']],


			['describe', 'required', null, '请填写卖点', ['save']],
			['describe', 'length', "1,40", '卖点中文在10个字符以内', ['save']],
			['buy_limit', 'GreaterThan', ['min' => 0, 'inclusive' => true], '限购数请正确填写', ['save']],
			['buy_price', 'GreaterThan', ['min' => 1, 'inclusive' => true], '活动价不正确', ['save']],

			['start_stock', 'ArrayMinlength', ['min' => 1], '请设置库存', ['save']],
			['start_stock', 'required', null, '请设置库存', ['save']],
			['start_stock', 'Callback', ['callback' => [new self(), 'checkStock']], '请设置库存', ['save']],
		];
	}

	public function checkStock($value, $valueChecker, $allData) {
		foreach ($value as $item) {
			if ($item < 0) {
				return false;
			}
			return true;
		}
		return false;
	}

	public function checkGoodsId($value, $valueChecker, $allData) {
		$goodsInfo = sGoods::Goods([$allData['goods_id']], sGoods::FIELDS_GROUP_DETAIL, ['activity_type' => ActivityInfo::ACTIVITY_TYPE_LIMIT_TIME_ROB]);
		if (!isset($goodsInfo[$allData['goods_id']]) || $goodsInfo[$allData['goods_id']]['status'] != mGoodsBasicInfo::STATUS_APP_NOMAL) {
			return false;
		}
		return true;
	}

	public function checkGoodsActivityTimer($value, $valueChecker, $allData) {
//		list($goodsIds, $invalidData) = AllActivityInfo::filterGoods([$allData['goods_id']],['start_time' => $allData['start_time'], 'end_time' => $allData['end_time']]);
        list($goodsIds, $invalidData) = AllactivityInfoBak::admCheckActivityTime(
            [$allData['goods_id']],
            ['start_time' => $allData['start_time'], 'end_time' => $allData['end_time']],
            ActivityInfo::ACTIVITY_TYPE_LIMIT_TIME_ROB,
            isset($allData['flashsaleid']) ? $allData['flashsaleid'] : 0
        );

		if ($invalidData) {
			if (!isset($allData['flashsaleid'])) return false;
			foreach ($invalidData as $datum) {
				if ($allData['flashsaleid'] && ($datum['type'] != AllActivityInfo::ACTIVITY_TYPE_LIMIT_TIME_ROB && $datum['activity_id'] != $allData['flashsaleid'])) {
					return false;
				}
			}
		}
		return true;
	}

	public function checkTimer($value, $valueChecker, $allData) {
		if ($allData['end_time'] < $allData['start_time']) {
			return false;
		}
		$info = mFlashSale::getFalseSaleIngInfo($allData['start_time']);
		if ( $info && isset($allData['flashsaleid']) && $info['id'] != $allData['flashsaleid']) {
			return false;
		}
		$info = mFlashSale::getFalseSaleIngInfo($allData['end_time']);
		if ( $info  && isset($allData['flashsaleid']) && $info['id'] != $allData['flashsaleid']) {
			return false;
		}
		return true;
	}
}